package com.oa.ylms.apps.dao.goods.impl;

import java.util.List;

import org.hibernate.FlushMode;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.goods.IGoodsInfoStates;
import com.oa.ylms.apps.dao.goods.IGoodsCategoryDao;
import com.oa.ylms.apps.entity.goods.GoodCategoryValue;

public class GoodsCategoryDaoImpl extends HibernateDaoSupport implements
		IGoodsCategoryDao {

	@SuppressWarnings("unchecked")
	public List<GoodCategoryValue> queryAll(int usability) throws Exception {
		String hql = "";
		if (usability == IDEDStates.ENABLE) {
			hql = "from GoodCategoryValue gcv where gcv.states=1";
		}
		if (usability == IDEDStates.DISABLE) {
			hql = "from GoodCategoryValue gcv where gcv.states=0";
		}
		if (usability == IGoodsInfoStates.NOTBYSTATES) {
			hql = "from GoodCategoryValue gcv where gcv.states=1 or gcv.states=0";
		}
		return this.getHibernateTemplate().find(hql);
	}

	public int addGoodsCategory(GoodCategoryValue goods) throws Exception {
		if (goods != null) {
			this.getSession().setFlushMode(FlushMode.AUTO);
			this.getHibernateTemplate().save(goods);
			return goods.getCategoryId();
		}
		return 0;
	}

	@Override
	public GoodCategoryValue queryByCateId(GoodCategoryValue goodCategory) {
		if (goodCategory != null&&goodCategory.getCategoryId()!=null) {
			return (GoodCategoryValue) this.getHibernateTemplate().get(
					GoodCategoryValue.class, goodCategory.getCategoryId());
		}
		return null;
	}

	@Override
	public boolean editGoodsCategory(GoodCategoryValue goodCategory) {
		if (goodCategory != null) {
			try {
				this.getHibernateTemplate().setFlushMode(2);
				this.getHibernateTemplate().update(goodCategory);
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}

}
